package com.cskaoyan.service.mall;

import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketCategory;
import com.cskaoyan.bean.po.MarketCategoryExample;
import com.cskaoyan.bean.vo.mall.CategoryLabelValueVo;
import com.cskaoyan.mapper.MarketCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CategoryServicelmpl implements CategoryService {

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Override
    public CommonData<MarketCategory> queryCategoryList() {
        List<MarketCategory> marketCategories = marketCategoryMapper.selectCategoryList();
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andPidEqualTo(0);
        int total = (int) marketCategoryMapper.countByExample(marketCategoryExample);
        return new CommonData<>(total, 1, total, 1, marketCategories);
    }

    @Override
    public CommonData<CategoryLabelValueVo> queryLabelValue() {
        List<CategoryLabelValueVo> categoryLabelValueVoList = marketCategoryMapper.selectLabelValue();
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andPidEqualTo(0);
        int total = (int) marketCategoryMapper.countByExample(marketCategoryExample);
        return new CommonData<>(total, 1, total, 1, categoryLabelValueVoList);
    }

    @Override
    public MarketCategory createCategory(MarketCategory marketCategory) {
        marketCategory.setAddTime(new Date());
        marketCategory.setUpdateTime(new Date());
        int code = marketCategoryMapper.insertSelective(marketCategory);
        if (code == 0) {
            return null;
        }
        MarketCategory marketCategory1 = marketCategoryMapper.selectByPrimaryKey(marketCategory.getId());
        return marketCategory1;
    }

    @Override
    public int deleteCategory(Integer id) {
        int code = marketCategoryMapper.deleteByPrimaryKey(id);
        if (code==0){
            return 0;
        }
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andPidEqualTo(id);
        marketCategoryMapper.deleteByExample(marketCategoryExample);
        return code;
    }

    @Override
    public int updateCategory(MarketCategory marketCategory) {
        marketCategory.setUpdateTime(new Date());
        int code = marketCategoryMapper.updateByPrimaryKeySelective(marketCategory);
        return code;
    }
}
